﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_MainChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_CUTNO.OnClientTriggerClick = Window1.GetSaveStateReference(hid_CUTNO.ClientID, txt_CUTNO.ClientID, txt_ADDRES.ClientID, txt_CONTAT.ClientID, txt_COTYPE.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx?r=1", "选择客户");

                txt_Team_Mem.OnClientTriggerClick = Window1.GetSaveStateReference(hid_Team_Mem.ClientID, txt_Team_Mem.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectUsers.aspx", "选择团队成员");
                btnClose.OnClientClick = ActiveWindow.GetConfirmHideReference();
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Main PMain = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["id"])).ToFirst();
                if (PMain != null)
                {
                    txt_ADDRES.Text = PMain.ADDRES;
                    txt_CONTAT.Text = PMain.CONTAT;
                    txt_DESTON.Text = PMain.DESTON;
                    txt_COTYPE.Text = PMain.COTYPE;
                    txt_ReMark.Text = PMain.ReMark;
                    txt_Team_Mem.Text = PMain.Team_Mem;
                    txt_CUTNO.Text = PMain.CUTNAM;
                    hid_CUTNO.Text = PMain.CUTNUB;
                    Drp_STATUS.SelectedValue = PMain.STATUS;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            int returnvalue = 0;
            Proj_Main PMain = new Proj_Main();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                PMain.ID = int.Parse(Request["id"]);
                PMain.Attach();
                PMain.COTYPE = txt_COTYPE.Text;
                PMain.CONTAT = txt_CONTAT.Text;
                PMain.ADDRES = txt_ADDRES.Text;
                PMain.DESTON = txt_DESTON.Text;
                PMain.STATUS = Drp_STATUS.SelectedValue;
                PMain.Team_Mem = txt_Team_Mem.Text;
                PMain.ReMark = txt_ReMark.Text;
                PMain.CUTNAM = txt_CUTNO.Text;
                PMain.CUTNUB = hid_CUTNO.Text;
                if (Drp_STATUS.SelectedValue == "2")
                    PMain.FinshTime = DateTime.Now;
                returnvalue = TbsDbSession.Default.Update<Proj_Main>(PMain);
            }
            else
            {
                PMain.DeAttach();
                PMain.PRJNUB = DFieldSN.SN(EnumData.DataSNEunm.项目编号);
                PMain.COTYPE = txt_COTYPE.Text;
                PMain.CONTAT = txt_CONTAT.Text;
                PMain.ADDRES = txt_ADDRES.Text;
                PMain.DESTON = txt_DESTON.Text;
                PMain.Adder = CookInfo.EAdminTrueName();
                PMain.AddID = CookInfo.EAdminid();
                PMain.AddTime = DateTime.Now;
                PMain.STATUS = Drp_STATUS.SelectedValue;
                PMain.Team_Mem = txt_Team_Mem.Text;
                PMain.ReMark = txt_ReMark.Text;
                PMain.CUTNAM = txt_CUTNO.Text;
                PMain.CUTNUB = hid_CUTNO.Text;
                if (Drp_STATUS.SelectedValue == "2")
                    PMain.FinshTime = DateTime.Now;
                returnvalue = TbsDbSession.Default.Insert<Proj_Main>(PMain);
            }
            if (returnvalue > 0)
            {
                string telstring = "";
                string team = hid_Team_Mem.Text;
                //,14,,13,
                List<string> ls = StringHelper.GetSubStringList(team, ',');
                foreach (string item in ls)
                {
                    HR_EMP he = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.ID == item).ToFirst();
                    if (he != null)
                    {
                        string content = string.Format("{0}，您已被加入到{1}项目，请及时关注。", he.EMNAME, PMain.DESTON);
                        // telstring += he.Phone + ",";
                        if (he.UEMAIL != "" && he.UEMAIL != null)
                            ToolHelper.sendMail(he.UEMAIL, he.EMNAME + "，您已经被加入" + PMain.DESTON + "项目，请及时关注", "项目参与提醒");
                        if (he.Phone != "" && he.Phone != null)
                            ToolHelper.sendTelMes(content, he.Phone);
                    }
                }

                //string t = string.Join(",", ls);
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference() + "top.__doPostBack('hid_btn', '');");
            }
        }

        protected void Drp_STATUS_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Alert.Show("1");
            if (Drp_STATUS.SelectedValue == "2")
                btnSave.ConfirmText = "您确定要设置项目为完成状态吗？";
            else
                btnSave.ConfirmText = "您确定以该处理状态保存吗？";
            //btnSave.OnClientClick = Confirm.GetShowReference("您确定要设置项目为完成状态吗？", "提示", MessageBoxIcon.Warning);
        }

    }
}